;(function($){
	var main = function(){
		var BussList;
		var ary=[];//定义一个空数组存外卖最小值;
		var size=100;//随机数最大值
		var num;//存储随机数的最小值
		var dish;
		var floatHTML = "\
			<div class='tooltip tooltip-placeleft' style='left:-82px;top:topvalue'>\
				<div class='tooltip-arrow'></div>\
				<div class='tooltip-content'>keyword</div>\
			</div>";
		//下面的a标签的href改为目标jsp
		var shopHTML = '<a href="//localhost:8080/eleme/shopdtl?shopid=getshopid" class="shopcard" shopid="getshopid">\
							<div class="shopcard-logo">\
								<img src="imgsrc"  width="70px" height="70px" class="shopcard-logo-icon">\
								<span>gettime分钟</span>\
								<div class="shopcard-logo-premiumsign"></div>\
							</div>\
							<div class="shopcard-content">\
								<div class="shopcard-title">shopname</div>\
								<div class="starrat icon-star">\
									<span class="icon star" style="92%"></span>\
									<span class="shopcard-mouthsales">月售salenum单</span>\
									<div class="shopcard-cost">配送费￥fee</div>\
									<div class="shopcard-activity">\
										<i style="background: #fff;color: #999999;border: 1px solid;padding: 0;">保</i>\
									</div>\
								</div>\
							</div>\
							<div class="shopcard-recommen placeright placeleft alignbottom" style="opacity: 1;">\
								<div class="tooltip-left-arrow">\
									<div class="tooltip-left-arrow-small"></div>\
								</div>\
								<div class="shopcard-recommen-title">shopname</div>\
								<div class="shopcard-recommen-flavors"></div>\
								<ul class="shopcard-recommen-activities"></ul>\
								<ul class="shopcard-recommen-delivery">\
									<li>配送费￥fee</li>\
									<li>平均<span class="color-stress plrtiny">gettime</span>分钟送达</li>\
								</ul>\
								<div class="shopcard-recommen-notice">introduce</div>\
							</div>\
						</a>';
//floatHTML是一段侧边栏悬浮的HTML代码块,为了方便就这样写了,行末的\是为了转义换行

//		初始化
		var loadEvent = function(){
			$(".place-fetchtakeout").on("click",whoshow);//展示谁去拿外卖层
			$(".fetchtakeout-close ").on("click",closewho);//隐藏谁去拿外卖
			$(".fetchtakeout-btn").on("click",produceNumber);//创建谁去拿外卖随机数
			
			$(".toolbar-open").on("click",toggleSideBar);//侧边栏点击事件初始化
			showBox('.actGotop','回到顶部','50px');//回到顶部悬浮事件
			showBox('.modal-hide','我的信息','150px');//我的信息悬浮事件
			showBox('.toobar-close','我的订单','10px');//我的订单悬浮事件
			$('.filter-menu .filter-item').on("click",mainMenuClick);//子菜单展示
		}
		
		
//		谁去拿外卖
//		背景层覆盖页面
		var whoshow = function(){
			$(".bgimg").show();
			$(".fetchtakeout-dialog").show();
		}
		
		var closewho = function(){
			$(".bgimg").hide();
			$(".fetchtakeout-dialog").hide();
			$(".fetchtakeout-list li").remove();//清除之前生成的随机数
			$(".fetchtakeout-emptytext").show();
			ary = [];//清空数组
			size = 100;
		}
		
//		创建一个数并展示
		var produceNumber = function() {
			$(".fetchtakeout-emptytext").hide();
			$(".fetchtakeout-list").show();
			removeRepeat();
			console.log(heightNum);
			$(".fetchtakeout-list li").remove();
			for(var i in ary){
				if(ary[i]==size){
					$(".fetchtakeout-list").append("<li class='weiimage'>扔出了一个"+ary[i]+"</li>");
				}else{
					$(".fetchtakeout-list").append("<li>扔出了一个"+ary[i]+"</li>");
				}
			}
			if(heightNum){
				$(".fetchtakeout-list").scrollTop(16 * heightNum);
			}
			
		}
		
//		随机数去重
		var removeRepeat = function(){
			var len = ary.length;
			if(len == 100){
				return;
			}
			num = parseInt(Math.floor(Math.random() * 100));
			for(var i = 0; i <= len; i++){
				if(ary[i] == num){
					removeRepeat();
					return;
				}else if(i==len){
					ary.push(num);
					if(num < size){
						size = num;
						heightNum = len;
					}
				}
			}
		};
		

		//点击事件
		var mainMenuClick = function(){

			var mainMenu = $(".filter-menu .filter-item");
			var len = mainMenu.length;
			$(".filter-menu .filter-item").removeAttr("style");
			for(var i = 0; i < len; i++){
				if(mainMenu[i]===this){
					$(".filter-subbox").empty();
					for(var j = 0; j < BussList[i].lstChild.length; j++){
						$(".filter-subbox").append($('<a class="filter-item" href="JavaScript:" bussid="'+BussList[i].lstChild[j].id+'">'+BussList[i].lstChild[j].name+'</a>'));
					}
				}
			}
			$('.filter-subbox').show();
			$(".filter-subbox .filter-item").removeAttr("style");
			$(".filter-subbox .filter-item:eq(0)").css("background-color","#008de6").css("color","#ffffff");
			$(this).css("background-color","#f6f6f6");
			$(".filter-subbox .filter-item").on("click",colorchange);//菜单点击变蓝
			shopmenu();
			
		}
		
		var colorchange = function(){
			$(".filter-subbox .filter-item").removeAttr("style");
			$(this).css("background-color","#008de6").css("color","#ffffff");

		}

//		商家右悬浮框
		var validateDescHover = function(){
			var tooltip; 
			var dish = $(".shopcard");
			dish.on('mousemove',function(){
				tooltip = $(this).find(".shopcard-recommen").eq(0);
				tooltip.stop(true).fadeIn(300);
				var thisx = event.clientX - $(this).offset().left;
				if(thisx < 0 || thisx > $(this).width()){
					tooltip = $(this).find(".shopcard-recommen").eq(0);
					tooltip.stop(true).fadeOut(10);
				}
			});
		}
		
//		侧边栏弹出与缩进
		var toggleSideBar = function(){

			var frame = $('.ng-car , .ng-message');
				frame.hide();
			var opencell = $(".toolbar-open");
			var len = opencell.length;
			if(this.open){
				$(".sidebar").animate({right: '-295px'},300);
				for(var i = 0; i < len; i++){
					 opencell[i].open = false;
					 $(opencell[i]).removeAttr("style");//移除两个按钮的行间样式
					if (opencell[i] === this){
						$(opencell[i]).css('color','#fff').css('background','#26a2ff');	//点击谁,谁的行间样式变为蓝色
						$(frame[i]).show();//点击第几个按钮,展示第几个div
					}
				}
				this.open = false;
			}else{
				$(".sidebar").animate({right: '-1px'},300);
				for(var i = 0; i < len; i++){
					opencell[i].open = false;
					$(opencell[i]).removeAttr("style");
					if (opencell[i] === this){
						$(opencell[i]).css('color','#fff').css('background','#26a2ff');	
						$(frame[i]).show();
					}
				}
				this.open = true;
			}			
		}

//		一键置顶
		var validBacktotop = function(){	
			$(window).scroll(function() {		
				if($(window).scrollTop() >= 100){
					$('.actGotop').fadeIn(300); 
				}else{    
					$('.actGotop').fadeOut(300);    
				}  
			});
			$('.actGotop').click(function(){$('html,body').animate({scrollTop: '0px'}, 800);}
			);
		};
		
		
//		侧边栏鼠标移入浮出提示块
		var showBox = function(sel,keyword,topvalue){
			var keynode = $(sel);
			keynode.hover(function(){
				keynode.after(floatHTML.replace('keyword',keyword).replace('topvalue',topvalue));
			},function(){
				$(sel + '+.tooltip').remove();
			})
		}
		
		var appendShop = function(name,time,fee,imagesrc,salenum,introduce){
			shopHTML.replace('time',time).replace('time',time);
			return shopHTML;
		}
		
		var buss = function(){
			$.ajax({
				type:"post",
				url:"http://localhost:8080/eleme/shopmain",
				dataType:"JSON",
				scriptCharset: 'utf-8', 
				async:false,//同步加载，数据加载完毕再执行后面的语句
				success:function(res){
					BussList=res;
					for(var i=0;i<BussList.length;i++){
						$(".filter-menu").append($('<a href="JavaScript:" class="filter-item" bussid="'+BussList[i].id+'" >'+BussList[i].name+'</a>'));	
					}
				}
			})
		}
		
		var showall = function(bussidObj){
			$.ajax({
				type:"POST",
				url:"http://localhost:8080/eleme/shopshow",
				dataType:"JSON",
				data:bussidObj,
				scriptCharset: 'utf-8', 
				success:function(res){
					console.log(res);
					var len = res.length;
					$('.place-rstbox .clearfix').empty();
					for(var i = 0; i < len; i++){
						var newHTML = shopHTML;
						newHTML = newHTML.replace(/gettime/g,res[i].avgdlvtime).replace(/shopname/g,res[i].name).replace(/salenum/g,res[i].monthlysold).replace(/fee/g,res[i].deliverfee).replace(/introduce/g,res[i].dtldesc).replace(/getshopid/g,res[i].id).replace('imgsrc',res[i].logo);
						$('.place-rstbox .clearfix').append(newHTML);
						
						if(i%4===3){
							$('.place-rstbox .clearfix').find('.shopcard:last-of-type').addClass("hoverleft");
							 $(".hoverleft .tooltip-left-arrow").attr("class", "tooltip-right-arrow");
							 $(".hoverleft .tooltip-left-arrow-small").attr("class", "tooltip-right-arrow-small");
						}
						
					}
					validateDescHover();
					
				}
			})
		}
				
				var shopmenu=function(){
					$(".filter-item").click(function(){
					var d = {"bussid": $(this).attr("bussid")};
					showall(d);
					})		
				}
				
				if("<%=sessionValues%>"=='null'){
				    showLogin();
				             bingLogin();   
				}else{
				  hiddenLogin();  
				} 
		
		return {
			run: function(){
				buss();
				showall({'bussid':1});
				loadEvent();
				validBacktotop();
				shopmenu();
			}
		}
	}();
	
	main.run();

})(jQuery);

